package com.imay.live.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.imay.live.adapter.RoomAdminAdapter;
import com.imay.live.proto.ErrorResult;
import com.imay.live.proto.ResultCode;
import com.imay.live.proto.helper.ProtoCallBack;
import com.imay.live.proto.helper.ProtoRequestHelper;
import com.imay.live.proto.helper.ProtoSocketFactory;
import com.imay.live.proto.room.RoomGetAdminListResp;
import com.imay.live.proto.room.RoomSetAdminResp;
import com.imay.live.proto.user.UserInfoCard;
import com.imay.live.view.recyclerview.WrapContentLinearLayoutManager;
import com.imay.live.R;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

/**
 * Created by jeaman on 2017/5/17.
 */

public class RoomAdminActivity extends BaseRequestActivity {
    private static final String EXTRA_NAME_ROOM_ID = "room_id";
    private final static int MAX_NUMBER = 10;

    private RecyclerView mRecyclerView;
    private TextView mNumberTv;

    private long mRoomId;
    private RoomAdminAdapter mAdapter;

    public static void startActivity(Context context, long roomId) {
        Intent intent = new Intent(context, RoomAdminActivity.class);
        intent.putExtra(EXTRA_NAME_ROOM_ID, roomId);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_room_admin;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mRoomId = getIntent().getLongExtra(EXTRA_NAME_ROOM_ID, 0);

        mRecyclerView = $(R.id.room_admin_rv_recycler);
        mNumberTv = $(R.id.room_admin_tv_number);

        setTitle(R.string.room_admin_title);

        mAdapter = new RoomAdminAdapter(null);
        mRecyclerView.setLayoutManager(new WrapContentLinearLayoutManager(this));
        mRecyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this).colorResId(R.color.divider_f8)
                .sizeResId(R.dimen.divider_size_admin)
                .showLastDivider()
                .build());
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setAdapter(mAdapter);

        mRecyclerView.addOnItemTouchListener(new OnItemChildClickListener() {
            @Override
            public void onSimpleItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                UserInfoCard user = mAdapter.getItem(position);
                requestRemoveAdmin(position, user.uid);
            }
        });

        setAdminNumber();
        requestData();
    }

    private void setAdminNumber() {
        mNumberTv.setText(getString(R.string.room_admin_number, mAdapter.getItemCount(), MAX_NUMBER));
    }

    private void requestData() {
        ProtoRequestHelper.task(ProtoSocketFactory.requestGetAdminList(mRoomId)).setTag(this.hashCode()).request(new ProtoCallBack() {
            @Override
            public void onRequestSuccess(String command, String data) {
                RoomGetAdminListResp resp = JSON.parseObject(data, RoomGetAdminListResp.class);
                if (resp.result == ResultCode.CodeSuccess && resp.users != null) {
                    mAdapter.setNewData(resp.users);
                    setAdminNumber();
                }
            }

            @Override
            public void onRequestError(String command, int code, String message) {
                ErrorResult.toastError(RoomAdminActivity.this, code);
            }
        });
    }

    private void requestRemoveAdmin(final int position, long uid) {
        ProtoRequestHelper.task(ProtoSocketFactory.requestRemoveAdmin(mRoomId, uid)).setTag(this.hashCode()).request(new ProtoCallBack() {
            @Override
            public void onRequestSuccess(String command, String data) {
                RoomSetAdminResp resp = JSON.parseObject(data, RoomSetAdminResp.class);
                if (resp.result == ResultCode.CodeSuccess) {
                    mAdapter.remove(position);
                    setAdminNumber();
                }
            }

            @Override
            public void onRequestError(String command, int code, String message) {
                ErrorResult.toastError(RoomAdminActivity.this, code);
            }
        });
    }
}
